/**
 * 设计循环队列
 * LeetCode中《队列&栈》学习中的设计题目
 * 缺点为大小固定
 */
public class LC_MyCircularQueue {

    public static void main(String[] args) {
        MyCircularQueue myCircularQueue = new MyCircularQueue(5);
        System.out.println("循环队列是否为空:" + myCircularQueue.isEmpty() + " 循环队列是否为满:" + myCircularQueue.isFull());
        System.out.println("添加数字是否成功:" + myCircularQueue.enQueue(6));
        System.out.println("循环队列是否为空:" + myCircularQueue.isEmpty() + " 循环队列是否为满:" + myCircularQueue.isFull());
        System.out.println(myCircularQueue.Front() + "  " + myCircularQueue.Rear());
    }

    /**
     * 使用数组作为队列，设置头指针，尾指针，根据设置长度设定循环队列大小
     * 头尾指针初始默认为-1，此时循环队列中没有值
     */
    static class MyCircularQueue{
        private int[] queue;
        private int head;
        private int tail;
        private int size;
        public MyCircularQueue(int k) {
            queue = new int[k];
            head = tail = -1;
            size = k;
        }

        //在尾部添加值
        public boolean enQueue(int value) {
            if (isFull()) {
                return false;
            }
            if (isEmpty()) {
                head = 0;
            }
            tail = (tail + 1) % size;
            queue[tail] = value;
            return true;

        }

        //在头部删除值
        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            if (head == tail) {
                head = tail = -1;
                return true;
            }
            head = (head + 1) % size;
            return true;
        }

        //返回头部
        public int Front() {
            if (isEmpty()) {
                return -1;
            }
            return queue[head];
        }

        //返回尾部
        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            return queue[tail];
        }

        //判断是否为空
        public boolean isEmpty() {
            return head == -1;
        }

        //判断是否装满
        public boolean isFull() {
            return (tail + 1) % size == head;
        }
    }
}
